﻿// Decompiled with JetBrains decompiler
// Type: NetickEditor.NetickEditorAnimationUtils
// Assembly: Netick.Unity.Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 0658BB4A-05C4-428D-9BD6-82E94959F5A1
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Editor\Netick.Unity.Editor.dll

using Netick.Unity;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;

#nullable disable
namespace NetickEditor;

internal static class NetickEditorAnimationUtils
{
  public static bool SetupAnimator(NetworkAnimator obj)
  {
    SerializedObject serializedObject = new SerializedObject((Object) obj.gameObject);
    Animator component = obj.gameObject.GetComponent<Animator>();
    List<int> list = new List<int>();
    List<int> intList = obj.IndexToHash != null ? ((IEnumerable<int>) obj.IndexToHash).ToList<int>() : new List<int>();
    NetickEditorAnimationUtils.FindStates(component.runtimeAnimatorController as AnimatorController, list);
    obj.IndexToHash = new int[list.Count];
    for (int index = 0; index < list.Count; ++index)
      obj.IndexToHash[index] = list[index];
    bool flag = false;
    if (list.Count != intList.Count)
      flag = true;
    if (list.Count == intList.Count)
    {
      for (int index = 0; index < list.Count; ++index)
      {
        if (list[index] != intList[index])
          flag = true;
      }
    }
    if (flag)
    {
      serializedObject.ApplyModifiedProperties();
      EditorUtility.SetDirty((Object) obj);
    }
    return flag;
  }

  public static void FindStates(AnimatorController ac, List<int> list)
  {
    foreach (AnimatorControllerLayer layer in ac.layers)
    {
      NetickEditorAnimationUtils.GetHashes(list, ((Object) ac).name, layer.name, layer.stateMachine.states);
      NetickEditorAnimationUtils.GetNestedHashes(list, ((Object) ac).name, layer.name, layer.stateMachine.stateMachines);
    }
  }

  private static void GetNestedHashes(
    List<int> hashlist,
    string acName,
    string path,
    ChildAnimatorStateMachine[] casm)
  {
    foreach (ChildAnimatorStateMachine animatorStateMachine in casm)
    {
      string path1 = $"{path}.{((Object) ((ChildAnimatorStateMachine) ref animatorStateMachine).stateMachine).name}";
      NetickEditorAnimationUtils.GetHashes(hashlist, acName, path1, ((ChildAnimatorStateMachine) ref animatorStateMachine).stateMachine.states);
      NetickEditorAnimationUtils.GetNestedHashes(hashlist, acName, path1, ((ChildAnimatorStateMachine) ref animatorStateMachine).stateMachine.stateMachines);
    }
  }

  private static void GetHashes(
    List<int> hl,
    string acName,
    string path,
    ChildAnimatorState[] states)
  {
    foreach (ChildAnimatorState state in states)
    {
      int hash1 = Animator.StringToHash(((Object) ((ChildAnimatorState) ref state).state).name);
      int hash2 = Animator.StringToHash($"{path}.{((Object) ((ChildAnimatorState) ref state).state).name}");
      if (!hl.Contains(hash1))
        hl.Add(hash1);
      if (hl.Contains(hash2))
        Debug.LogError((object) $"Error on configuring animator [{acName}]: repeated state name [{((Object) ((ChildAnimatorState) ref state).state).name}]. Make sure there are no repeated state names by changing the similar ones.");
      else
        hl.Add(hash2);
    }
  }
}
